//
//  InstructionsLayer.m
//  SCRAP_ObjC
//
//  Created by Cezara Coroianu on 11/9/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "InstructionsLayer.h"
#import "MenuLayer.h"
#import "StackAdditions.h"
#import "Constants.h"


@implementation InstructionsLayer
@synthesize menuSprites, menuIndex, instrMenu, backItem, nextItem;

+(id) scene{
	InstructionsLayer *scene = [InstructionsLayer node];
	return scene;
}
-(id) init{
	if((self =[super init]))
	{
		[[CCDirector sharedDirector] setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
		menuIndex = 0;
		menuSprites = [[NSMutableArray alloc] initWithCapacity:5];
		
		instr = [CCSprite spriteWithFile: @"default_menu.png"];
		
		size = [[CCDirector sharedDirector] winSize];
		center = ccp(size.width / 2, size.height / 2);
		
		//portals = [[NSMutableArray alloc] initWithCapacity: 4];
		
		[instr setPosition:center];
		/*
		CCLabel *menuLabel = [CCLabel labelWithString:@"Menu" fontName:SCRAPFont fontSize:26];
		CCLabel *nextLabel = [CCLabel labelWithString:@"Next" fontName:SCRAPFont fontSize:26];
		CCLabel *backLabel = [CCLabel labelWithString:@"Back" fontName:SCRAPFont fontSize:26];
		 */
		
		CCSprite *menuButton = [CCSprite spriteWithFile:@"up small.png"];
		CCSprite *menuButton_sel = [CCSprite spriteWithFile:@"up small_select.png"];
		CCSprite *nextButton = [CCSprite spriteWithFile:@"right small.png"];
		CCSprite *nextButton_sel = [CCSprite spriteWithFile:@"right small_select.png"];
		CCSprite *backButton = [CCSprite spriteWithFile:@"left small.png"];
		CCSprite *backButton_sel = [CCSprite spriteWithFile:@"left small_select.png"];
		
		CCMenuItemSprite *menuItem = [CCMenuItemSprite itemFromNormalSprite:menuButton selectedSprite:menuButton_sel target:self selector:@selector(home)];
		nextItem = [CCMenuItemSprite itemFromNormalSprite:nextButton selectedSprite:nextButton_sel  target:self selector:@selector(next)];
		backItem = [CCMenuItemSprite itemFromNormalSprite:backButton selectedSprite:backButton_sel  target:self selector:@selector(back)];
				
		backItem.visible = FALSE;
		
		instrMenu = [CCMenu menuWithItems:backItem, menuItem, nextItem, nil];
		instrMenu.position = ccp(center.x, 25);
		
		[instrMenu alignItemsHorizontallyWithPadding:40];		
		
		CCSprite *menuSprite1 = [CCSprite spriteWithFile:@"reserve_1.png"];
		menuSprite1.position = ccp(960, center.y);
		CCSprite *menuSprite2 = [CCSprite spriteWithFile:@"waste_1.png"];
		menuSprite2.position = ccp(960, center.y);
		CCSprite *menuSprite3 = [CCSprite spriteWithFile:@"cascade_1.png"];
		menuSprite3.position = ccp(960, center.y);
		CCSprite *menuSprite4 = [CCSprite spriteWithFile:@"portal_1.png"];
		menuSprite4.position = ccp(960, center.y);
		
		[menuSprites push:instr];
		[menuSprites push:menuSprite1];
		[menuSprites push:menuSprite2];
		[menuSprites push:menuSprite3];
		[menuSprites push:menuSprite4];
		
		for(CCSprite *s in menuSprites)
			[self addChild:s];
		
		[self addChild:instrMenu];
	}
	return self;
}
-(void) home{
	//[[CCDirector sharedDirector] replaceScene:[MenuLayer node]];
	[[CCDirector sharedDirector] replaceScene:[CCFadeTRTransition transitionWithDuration:.5 scene:[MenuLayer node]]];
}
-(void) next{
	menuIndex++;
	if(menuIndex >= [menuSprites count] - 1)
	{
		menuIndex = [menuSprites count] - 1;
		nextItem.visible = FALSE;
	}
	
	CCSprite *currentSprite = (CCSprite*)[menuSprites objectAtIndex:menuIndex];
	
	id action = [CCMoveTo actionWithDuration:.5 position:center];
	id ease = [CCEaseIn actionWithAction:action rate:.5];
	[currentSprite runAction:ease];
	backItem.visible = TRUE;
}
-(void)back{
	if(menuIndex == 0)
	{
		return;
	}
	
	CCSprite *currentSprite = (CCSprite*)[menuSprites objectAtIndex:menuIndex];
	
	id action = [CCMoveTo actionWithDuration:.5 position:ccp(960, center.y)];
	id ease = [CCEaseIn actionWithAction:action rate:.5];
	[currentSprite runAction:ease];
	menuIndex--;
	if(menuIndex <= 0)
	{
		menuIndex = 0;
		backItem.visible = FALSE;
	}
	nextItem.visible = TRUE;
}

-(void) dealloc
{
	[super dealloc];
}

@end
